import Vue from 'vue'
export default {
	
	/* 属性值 */
  state: {
		//是否在wss服务中注册
		isOnline:false,
		//wss连接是否打开
		isSocketOpen:false,
		//是否在播放音频
		isAudioPlay:false,
		// 用户信息
		user_info:{},
		// 用户列表
    userListObject:{},
		//群聊消息列表
		groupMassageList:{},
		//私聊消息列表
		privateMassagerList:[],
		//私聊对象ID
		private_id:0,
		//私聊消息提示列表
		privateMassagerTips:{},
		//好友列表
		relationList:[],
		//好友请求列表
		requestRelationList:[],
		//好友请求列表的用户信息
		requestRelationUserInfo:{}
  },
	
	/* 
		计算属性 
		定义 doneTodos:(state, getters,rootState)=>{}
		使用 store.getters.doneTodosCount
	*/
  getters: {
		privateMassagerObject(state, getters,rootState){
			let obj={},uid =state.user_info.id*1||null
			if(!uid){return {}}
			state.privateMassagerList.forEach((item)=>{
				let key = item.send_id*1
				if(key === uid){
					key= '_chat_new_'+item.receive_id
				}else{
					key= '_chat_new_'+item.send_id
				}
				obj[key]=obj[key]||[];
				obj[key].push(item);
			})
			return obj
		}
  },
	/* 
		计算方法
		定义 increment :function(state,data){}
		使用 store.commit('increment', data) or  store.commit({type:'increment',...data})
	 */
  mutations: {
		//设置用户信息
		setChatUserInfo(state,data){
			state.user_info=data
		},
		//设置wss打开状态
		changeSocket(state,data){
			if(!data){
				state.isOnline=false
			}
			state.isSocketOpen=data
		},
		//设置播放状态
		audioPlay(state,data){
			state.isAudioPlay=data
		},
		
		/* 以下是聊天室操作 */
		//用户上线
		receiveChatOnLine(state){
			state.isOnline=true
		},
		//用户下线
		receiveChatOffLine(state){
			state.isOnline=false
		},
		// 设置私聊ID
		changPrivateiId(state,data){
			state.private_id=data
		},
		// 当有用户登录广播时记录
		receiveChatOnOpen(state,data){
			let {user_info}=data;
			if(user_info){
				let {id}=user_info
				if(id ){
					let key='_chat_new_'+id
					Vue.set(state.userListObject,key,user_info)
				}
			}
		},
		// 拉取用户列表
		receiveChatUserList(state,data){
			state.userListObject=data.user_list||{}
		},
		//记录单条聊天记录
		receiveChatPrivate(state,data){
			state.privateMassagerList.push(data)
			if(data.send_id != state.user_info.id){
				Vue.set(state.privateMassagerTips,data.send_id,true)
			}
		},
		//取消新消息提醒
		cancelMassagerTips(state,id){
			Vue.set(state.privateMassagerTips,id,false)
		},
		// 更新两个人的私聊信息
		receiveChatPrivateList(state,data){
			for (let  i = state.privateMassagerList.length - 1; i >= 0; i--) {
				let item=state.privateMassagerList[i];
				//console.log(data.receive_id,data.send_id,item.receive_id,item.send_id)
				/*   有时会出现id是字符串 */
				let r1=(item.receive_id==data.receive_id) && (item.send_id==data.send_id);
				let r2=(item.receive_id==data.send_id) && (item.send_id==data.receive_id);
				if(r1||r2){
					state.privateMassagerList.splice(i,1); 
				}
			}
			data.list.forEach(item=>{
				if(typeof item =='string'){
					state.privateMassagerList.push(JSON.parse(item));
				}
			})
		},
		//添加好友请求
		receiveRequestRelation(state,data){
			let {send_id,message}=data
			send_id='_chat_new_'+send_id
			let index=state.requestRelationList.findIndex(item=> item.send_id===send_id)
			if(index>-1){
				state.requestRelationList.splice(index,1,{send_id,message})
			}else{
				state.requestRelationList.push({send_id,message})
			}
		},
		//刷新好友请求
		receiveRequestRelationList(state,data){
			let arr=[]
			for (let key in data.list) {
				arr.push({send_id:key,message:data.list[key]})
			}
			state.requestRelationList=arr
		},
		// 请求好友的用户信息
		receiveRequestRelationUserInfo(state,data){
			state.requestRelationUserInfo=data.user_list||{}
		},
		//好友请求处理回调
		receiveManageRelation(state,data){
			console.log(data)
		},
		receiveGetRelation(state,data){
			state.relationList=data.user_list||[]
		},
	},
	/* 
		导步计算方法
		定义 incrementAsync  :function ({ commit, state,dispatch,rootState  },data){commit('increment',data)}
		使用 store.dispatch('incrementAsync', data) or  store.commit({type:'incrementAsync',...data})
	 */
  actions: {
		
  }
}